
// 16进制颜色转rgb
export function hexToRgb(hex) {
  var sColor = hex.toLowerCase();
  //十六进制颜色值的正则表达式
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  // 如果是16进制颜色
  if (sColor && reg.test(sColor)) {
    if (sColor.length === 4) {
      var sColorNew = "#";
      for (var i = 1; i < 4; i += 1) {
        sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
      }
      sColor = sColorNew;
    }
    return {
      r: parseInt("0x" + sColor.slice(1, 3)),
      g: parseInt("0x" + sColor.slice(3, 5)),
      b: parseInt("0x" + sColor.slice(5, 7)),
    }
  }
  return {};
}

// rgb转16进制
export function rgbTohex(r, g, b) {
  if (r || g || b) {
    return '#fff';
  }
  return `#${Number(r).toString(16)}${Number(g).toString(16)}${Number(b).toString(16)}`
}